package com.yyf.springboot2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yyf.springboot2.entity.TMoneyyj;
import com.yyf.springboot2.entity.TUser;
import com.yyf.springboot2.mapper.TMoneyyjMapper;
import com.yyf.springboot2.mapper.TUserMapper;
import com.yyf.springboot2.service.TMoneyyjService;
import com.yyf.springboot2.service.TUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yyf
 * @since 2019-05-29
 */
@Service
@Transactional
public class TUserServiceImpl extends ServiceImpl <TUserMapper, TUser> implements TUserService {

    @Resource
    private TUserMapper userMapper;


    @Resource
    private TMoneyyjService moneyyjService;


    @Override
    public TUser getUserByUsername(String userName) {
        QueryWrapper <TUser> queryWrapper = new QueryWrapper <TUser>();
        queryWrapper.select("id", "username", "jhTime", "type", "stat", "ip", "isBaoDan", "isDj",
                "lastIP", "lastTime", "loginCount", "mail", "moneyMy", "moneyScore", "moneyTz", "name", "password", "tjId",
                "tjUsername", "zType", "yType");
        queryWrapper.eq("username", userName);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateLastTimeAndLoginCountAndLastIPById(Integer id, String ip) {
        TUser user = userMapper.selectById(id);
        user.setLastIP(ip);
        user.setLastTime(new Date());
        UpdateWrapper <TUser> updateWrapper = new UpdateWrapper <TUser>();
        updateWrapper.eq("id", user.getId());
        Integer result = userMapper.update(user, updateWrapper);
        return result;
    }

    @Override
    public TUser getUserInfoById(Integer id) {
        QueryWrapper <TUser> queryWrapper = new QueryWrapper <TUser>();
        queryWrapper.select("id", "username", "jhTime", "type", "stat", "ip", "isBaoDan", "isDj",
                "lastIP", "lastTime", "loginCount", "mail", "moneyMy", "moneyScore", "moneyTz", "name", "tjId",
                "tjUsername", "zType", "yType");
        queryWrapper.eq("id", id);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int updatePassAndSalt(String password, String salt, Integer userId) {
        return userMapper.updatePdAnfSalt(password, salt, userId);
    }

    @Override
    public TUser getUserAmount(Integer uid) {
        QueryWrapper <TUser> queryWrapper = new QueryWrapper <TUser>();
        queryWrapper.select("id", "username", "moneyMy", "moneyScore", "moneyTz",
                "regMoney", "jiJin", "guQuan", "guangGao", "guangGaoMoney", "totalMoney",
                "staticMoney", "isfanli", "loveMoney", "shengjiTz", "joinMoney", "user_cash", "scoreMoney"

        );
        queryWrapper.eq("id", uid);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer updateMoneyById(Map <String, Object> map) {
        return userMapper.updateMoneyById(map);
    }

    @Override
    public TUser getUserInfoByUsername(String userName) {
        QueryWrapper <TUser> queryWrapper = new QueryWrapper <TUser>();
        queryWrapper.select("id", "username", "jhTime", "type", "stat", "ip", "isBaoDan", "isDj",
                "lastIP", "lastTime", "loginCount", "mail", "moneyMy", "moneyScore", "moneyTz", "name", "tjId",
                "tjUsername", "zType", "yType");
        queryWrapper.eq("username", userName);

        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer moneyScoreChange(Map <String, Object> map) {

        TUser user = userMapper.selectById(1);
user.setSName("0");
        userMapper.update(user,new UpdateWrapper <TUser>().eq("id",1));
        return userMapper.changeMoneyScoreById(map);
    }

    @Override
    public Integer moneyScoreTransfel(Map <String, Object> map) {
        Integer userId = (Integer) map.get("userId");
        Integer toUserId = (Integer) map.get("toUserId");
        Double amount = (Integer) map.get("money")+0.0;
        TUser toUserInfo = getUserInfoById(userId);
        //获取会员账户余额
        TUser userAmount = getUserAmount(userId);
        if (map.get("type").equals(1)) {
            //获取比例，计算手续费
            Double shui = (Double) map.get("shui");

            Double money = amount - amount * shui;
            Map <String, Object> m = new HashMap <>();
            m.put("userId", userId);
            m.put("moneyMy", amount);
            Map <String, Object> p = new HashMap <>();
            p.put("userId", toUserId);
            p.put("moneyMy", money);
            List <TMoneyyj> moneyyjs = new ArrayList <>();
            //存明细
            TMoneyyj moneyyj = new TMoneyyj(userAmount.getMoneyMy() - amount,
                    amount, toUserId, 1, new Date(), 1, userId, null, "用户转账");
            //存明细
            TMoneyyj moneyyj1 = new TMoneyyj(toUserInfo.getMoneyMy() + amount,
                    amount, userId, 0, new Date(), 2, toUserId, null, "收到转账");
            //存明细
            TMoneyyj moneyyj2 = new TMoneyyj(toUserInfo.getMoneyMy() + money,
                    amount * shui, userId, 1, new Date(), 60, toUserId, null, "扣手续费");
            moneyyjs.add(moneyyj);
            moneyyjs.add(moneyyj1);
            moneyyjs.add(moneyyj2);
            //修改转账人账户信息
            Integer up = userMapper.updateMoneyById(m);
            //修改收款人账户余额信息
            Integer add = userMapper.updateMoneyByIdAdd(p);
            boolean result = moneyyjService.saveBatch(moneyyjs);
            m.clear();
            p.clear();
            if (up != 1 || add != 1 || result != true) {
                return -1;
            }
            return 1;


        } else if (map.get("type").equals(2)) {
            //获取比例，计算手续费
            Double shui = (Double) map.get("shui");
            Double money = amount - amount * shui;
            Map <String, Object> m = new HashMap <>();
            m.put("userId", userId);
            m.put("moneyScore", amount);
            Map <String, Object> p = new HashMap <>();
            p.put("userId", toUserId);
            p.put("moneyScore", money);
            List <TMoneyyj> moneyyjs = new ArrayList <>();
            //存明细
            TMoneyyj moneyyj = new TMoneyyj(userAmount.getMoneyScore() - amount,
                    amount, toUserId, 1, new Date(), 101, userId, null, "用户转账");
            //存明细
            TMoneyyj moneyyj1 = new TMoneyyj(toUserInfo.getMoneyScore() + amount,
                    amount, userId, 0, new Date(), 102, toUserId, null, "收到转账");
            //存明细
            TMoneyyj moneyyj2 = new TMoneyyj(toUserInfo.getMoneyScore() + money,
                    amount * shui, userId, 1, new Date(), 60, toUserId, null, "扣手续费");
            moneyyjs.add(moneyyj);
            moneyyjs.add(moneyyj1);
            moneyyjs.add(moneyyj2);
            //修改转账人账户信息

            Integer up = userMapper.updateMoneyById(m); // 此处方法可修改moneymy 、 moneyScore
            //修改收款人账户余额信息
            Integer add = userMapper.updateMoneyByIdAdd(p);
            boolean result = moneyyjService.saveBatch(moneyyjs);
            p.clear();
            if (up != 1 || add != 1 || result != true) {
                return -1;
            }
            return 1;


        } else {
            return null;
        }


    }
}
